/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.listener;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.EditPart;

import com.cea.papyrus.core.editpart.GraphNodeEditPart;
import com.cea.papyrus.core.editpart.IUMLElementEditPart;
import com.cea.papyrus.core.editpart.IUMLObjectEditPart;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * This class is used to create a listener on UML element and to call automatically editPart.refresh
 * when the UML element is modified
 */
public class UmlElementListener implements Adapter {

	/** target of the notifications */
	private Notifier _target;
	
	/** edit part that listens to the notifications */
	protected EditPart umlEditPart;

	/** debug channel */
	protected static final int channel = 300;

	/**
	 * Creates a listener on the umlElement.
	 * @param editPart the editpart of the UMLElement
	 */
	public UmlElementListener(IUMLObjectEditPart editPart){
		this.umlEditPart=(EditPart)editPart;
	}

	/**
	 * Notifies that a change to some feature has occurred.
	 * @param notification a description of the change.
	 */
	public void notifyChanged(Notification notification) {
		Debug.debug(this, ""+notification, channel);
		if (Notification.REMOVING_ADAPTER != notification.getEventType()
				&& Notification.REMOVE != notification.getEventType()
				&& PapyrusNotification.ELEMENT_DELETED != notification.getEventType()) {
			umlEditPart.refresh();	
			//FIXME : to delete when the modelmanager knows when uml model is modified.	
			if ( umlEditPart instanceof IUMLElementEditPart && notification.getEventType() != Notification.REMOVING_ADAPTER ) {
				((IUMLElementEditPart) umlEditPart).getModelManager().modifyUMLElement(((IUMLElementEditPart) umlEditPart).getUmlElement());
			}
			if ( (notification.getEventType() ==  Notification.SET) && (notification.getNotifier() instanceof Uml1SemanticModelBridge) ) {	
				if( umlEditPart instanceof GraphNodeEditPart){
					Debug.debug(this, "CHANGE FIGURE", channel);	
					((GraphNodeEditPart) umlEditPart).changeFigureToStereotypeFigure();
				}				
			}
		}
	}

	/**
	 * Returns the target from which the adapter receives notification.
	 * In general, an adapter may be shared by more than one notifier.
	 * @return the target notifier.
	 * @see #setTarget
	 */
	public Notifier getTarget() {
		return this._target;
	}

	/**
	 * Sets the target from which the adapter will receive notification.
	 * This method is only to be called by a notifier when this adapter
	 * is added to or removed from its adapter list.
	 * In general, an adapter may be shared by more than one notifier.
	 * @param newTarget the new notifier.
	 * @see #getTarget
	 */
	public void setTarget(Notifier newTarget) {
		this._target = newTarget;
	}

	/**
	 * Returns whether the adapter is of the given type.
	 * In general, an adapter may be the adapter for many types.
	 * @param type the type.
	 * @return whether the adapter is of the given type.
	 * @see AdapterFactory#isFactoryForType
	 */
	public boolean isAdapterForType(Object type) {
		return type.equals(umlEditPart.getModel().getClass());
	}

}